const { Controller } = require('egg');

class MemoController extends Controller {
    /**
     * 添加备忘录
     */
    async create() {
        console.log('ctx.params', this.ctx.params);

        // 获取参数
        const {
            platform,
            task,
            benjin,
            is_return_benjin,
            yongjin,
            is_return_yongjin,
            mark,
        } = this.ctx.request.body;

        console.log('this.ctx.request.body', this.ctx.request.body);

        // 非空校验
        if (!platform || !task || !benjin || !yongjin || !mark) {
            this.ctx.body = {
                code: 400,
                message: '缺少必须参数',
            };
            return;
        }

        // 判断布尔值
        if (isNaN(is_return_benjin) || ![0, 1].includes(is_return_benjin)) {
            this.ctx.body = {
                code: 400,
                message: '是否返回本金字段格式不正确',
            };
            return;
        }

        if (
            isNaN(is_return_yongjin) ||
            (is_return_yongjin !== 0 && is_return_yongjin !== 1)
        ) {
            this.ctx.body = {
                code: 400,
                message: '是否返回佣金字段格式不正确',
            };
            return;
        }

        // 平台只能是：淘宝、京东、拼多多
        if (
            platform !== '淘宝' &&
            platform !== '京东' &&
            platform !== '拼多多'
        ) {
            this.ctx.body = {
                code: 400,
                message: '平台只能选择：淘宝、京东、拼多多',
            };
            return;
        }

        // 调用 service 层，添加备忘录
        let res = await this.service.memo.create({
            platform,
            task,
            benjin,
            is_return_benjin,
            yongjin,
            is_return_yongjin,
            mark,
        });

        // 判断是否添加成功
        if (res.affectedRows !== 1) {
            this.ctx.body = {
                code: 500,
                message: '添加失败',
            };
            return;
        }

        // 添加成功
        this.ctx.body = {
            code: 201,
            message: '添加成功',
        };
    }

    /**
     * 删除备忘录
     */
    async destroy() {
        // 获取参数
        const { id } = this.ctx.params;
        // 删除
        let res = await this.service.memo.destroy(id);
        if (res.affectedRows !== 1) {
            this.ctx.body = {
                code: 500,
                message: '删除失败',
            };
            return;
        }
        this.ctx.body = {
            code: 204,
            message: '删除成功',
        };
    }

    /**
     * 修改备忘录
     */
    async update() {
        // 获取参数
        const { id } = this.ctx.params;

        let res = await this.service.memo.findById(id);

        if (!res) {
            this.ctx.body = {
                code: 404,
                message: '不存在',
            };
            return;
        }

        res = await this.service.memo.update({
            ...res,
            ...this.ctx.request.body,
        });

        if (res.affectedRows !== 1) {
            this.ctx.body = {
                code: 500,
                message: '更新失败',
            };
            return;
        }

        this.ctx.body = {
            code: 200,
            message: '更新成功',
        };
    }

    /**
     * 获取列表
     */
    async index() {
        let res = await this.service.memo.readList();
        this.ctx.body = {
            code: 200,
            data: res,
            message: '请求成功',
        };
    }

    /**
     * 获取详情
     */
    async detail() {
        // 获取 id
        const { id } = this.ctx.params;
        // 从数据库查出 id
        let res = await this.service.memo.findById(id);
        this.ctx.body = {
            code: 200,
            data: res,
            message: '请求成功',
        };
    }
}

module.exports = MemoController;
